package com.zhb.controller;

/**
 * Created by jarvan4dev on 16/5/21.
 */

import com.zhb.entity.ActivityInfo;
import com.zhb.entity.MyActivity;
import com.zhb.entity.UserInfo;
import com.zhb.services.ActivityService;
import com.zhb.services.UserService;
import com.zhb.util.IOUtils;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.IOException;
import java.util.List;

/**
 * 上下文controller, 托管httpClient
 * 保持httpClient单例, 即可保证当前登录态
 */
public class LibContextController {

    private HttpClient httpClient;

    private UserService userService;
    private ActivityService activityService;

    public LibContextController(){
        httpClient = HttpClients.createDefault();
        userService = new UserService(httpClient);
        activityService = new ActivityService(httpClient);
    }

    private void saveProfile(String username, String password) throws IOException {
        // 登录
        userService.doLoginLib(username, password);
        // 打开个人信息页面
        String html = userService.visitProfilePage();

        UserInfo userInfo = userService.getUserInfo(html);

        IOUtils.saveResult(userInfo, "userInfo.json");
    }

    private void saveMyActivityList() throws IOException {
        String html = activityService.visitMyActivityPage();
        // 起中包含了我参加的活动和该活动详情
        List<MyActivity> myActivityList = activityService.getMyActivities(html);

        IOUtils.saveResult(myActivityList, "myActivityList.json");
    }

    private void saveAllActivityList() throws IOException {
        String html = activityService.visitAllActivityPage();

        List<ActivityInfo> activityInfoList = activityService.getAllActivities(html);

        IOUtils.saveResult(activityInfoList, "activityInfoList.json");
    }


    public static void main(String[] args) throws IOException {
        LibContextController controller = new LibContextController();
        String username = "0004989";//IOUtils.getInput("请输入用户名");
        String password = "19801025";//IOUtils.getInput("请输入密码");
        controller.saveProfile(username, password);
        controller.saveAllActivityList();
    }
}
